import {login, logout, getInfo} from '@/api/user'
import {getToken, setToken, removeToken} from '@/utils/auth'
import {resetRouter} from '@/router'

const getDefaultState = () => {
    return {
        token: getToken(),
        name: '',
        avatar: '',
        account: '',
        number: 0,
        useNumber: 0,
        role: 0,
        roles: []
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_ACCOUNT: (state, account) => {
        state.account = account
    },
    SET_NUMBER: (state, number) => {
        state.number = number
    },
    SET_USED_NUMBER: (state, number) => {
        state.useNumber = number
    },
    SET_ROLE: (state, role) => {
        state.role = role
    },
    SET_ROLES: (state, roles) => {
        state.roles = roles
    }
}

const actions = {
    // user login
    login({commit}, userInfo) {
        const {username, password,visitorId} = userInfo
        return new Promise((resolve, reject) => {
            login({username: username.trim(), password: password,visitorId:visitorId}).then(response => {
                commit('SET_TOKEN', response.data.token)
                commit('SET_ROLES', response.data.roles)
                setToken(response.data.token)
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // get user info
    getInfo({commit, state}) {
        return new Promise((resolve, reject) => {
            getInfo().then(response => {
                if (!response.data) {
                    return reject('Verification failed, please Login again.')
                }

                commit('SET_NAME', response.data.name)
                commit('SET_AVATAR', 'https://customer-service-data.s3.ap-south-1.amazonaws.com/0c9c0d25df46f4fe4d532d8ba3a98389.jpg')
                commit('SET_ACCOUNT', response.data.account)
                commit('SET_NUMBER', response.data.integral)
                commit('SET_USED_NUMBER', response.data.used_integral)
                commit('SET_ROLE', response.data.role)
                commit('SET_ROLES', response.data.roles)
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // user logout
    logout({commit, state}) {
        return new Promise((resolve, reject) => {
            logout().then(() => {
                removeToken() // must remove  token  first
                resetRouter()
                commit('RESET_STATE')
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // remove token
    resetToken({commit}) {
        return new Promise(resolve => {
            removeToken() // must remove  token  first
            commit('RESET_STATE')
            resolve()
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

